import React from 'react';
import moment from 'moment';
import router from 'umi/router';
//import Link from 'umi/link'

import { connect } from 'dva';

import {  Button,Form, Input, Select,DatePicker } from 'antd';

const FormItem = Form.Item;

const { Option } = Select;


const linePath = '/AccountInvoice/Invoice';


@connect(({
  accountInvoice,accountJournal,resPartner,accountAccount
}) => ({
  accountInvoice,accountJournal,resPartner,accountAccount
}))
@Form.create()
class FormCreate extends React.Component {
  state = {
  }

  id = 0

  async componentDidMount() {
    const { location: {query: { id } } } = this.props;

    const { dispatch } = this.props;

    if (id) {
      this.id = Number(id)
      await dispatch({
        type: 'accountInvoice/view',
        payload:  { id:  this.id }
      });
    }

    await dispatch({
      type: 'accountJournal/search',
      payload: {domain:[] },
    });

    await dispatch({
      type: 'resPartner/search',
      payload: {domain:[] },
    });

    await dispatch({
      type: 'accountAccount/search',
      payload: {domain:[] },
    });

  }


  handleSubmit = e => {
    const { dispatch, form } = this.props;
    e.preventDefault();


    const create_or_write = async (vals) => {
      if(this.id){
        await dispatch({
          type: 'accountInvoice/write',
          payload : {
            id: this.id,  vals,
          },
        });

        router.push(`${linePath}?id=${this.id}`);
      }
      else{
        await dispatch({
          type: 'accountInvoice/create',
          payload : {
            vals,
          },
        });

        const { accountInvoice } = this.props;
        if (accountInvoice.id){
          router.push(`${linePath}?id=${accountInvoice.id}`);
        }
      }
    }


    form.validateFieldsAndScroll( async (err, values) => {
      if (!err) {
        const { date, journal_id, account_id } = values;

        const vals = {
          ...values,
          date: date.format('YYYY-MM-DD'),
        };

        if( ! journal_id ){
          //delete vals.journal_id;
        }

        if( ! account_id ){
          delete vals.account_id;
        }



        console.log( vals );

        await create_or_write(vals)

      }
    })
  }


  render() {
    const { form: { getFieldDecorator }  } = this.props;
    const { accountInvoice: { record } } = this.props
    const { accountJournal: { recordsList: journalList } } = this.props;
    const { resPartner: { recordsList: partnerList } } = this.props;

    const { accountAccount: { recordsList: accountList } } = this.props;




    return (
      <div>
          <Form onSubmit={this.handleSubmit} >
            <FormItem style={{ marginTop: 32 }}>
              <Button type="primary" htmlType="submit" >提交
              </Button>
            </FormItem>

            <FormItem label="类型">
              {getFieldDecorator('type', {
                rules: [{ required: true }],
                initialValue: record.type || 'out_invoice'
              })(
                <Select
                  placeholder="账簿" >
                  <Option value={'out_invoice'} >发票</Option>
                  <Option value={'in_invoice'} >账单</Option>
                  <Option value={'out_refund'} >红发票</Option>
                  <Option value={'in_refund'} >红账单</Option>
                </Select>

              )}
            </FormItem>

            <FormItem label="账簿">
              {getFieldDecorator('journal_id', {
                rules: [{ required: true }],
                initialValue: record.journal_id ? record.journal_id.id : 0
              })(
                <Select
                  placeholder="账簿" >
                  <Option value={0} >未选择</Option>
                  {journalList.map(item => {
                    return (
                      <Option value={item.id} key="id">
                        {`${item.code} ${item.name}`}
                      </Option>
                    );
                  })}

                </Select>

              )}
            </FormItem>

            <FormItem label="客户/供应商">
              {getFieldDecorator('partner_id', {
                rules: [{ required: true }],
                initialValue: record.partner_id ? record.partner_id.id : 0
              })(
                <Select
                  placeholder="客户/供应商" >
                  <Option value={0} >未选择</Option>
                  {partnerList.map(item => {
                    return (
                      <Option value={item.id} key="id">
                        {`${item.code} ${item.name}`}
                      </Option>
                    );
                  })}

                </Select>

              )}
            </FormItem>

            <FormItem label="科目">
              {getFieldDecorator('account_id', {
                rules: [{ required: true }],
                initialValue: record.account_id ? record.account_id.id : 0
              })(
                <Select
                  placeholder="客户/供应商" >
                  <Option value={0} >未选择</Option>
                  {accountList.map(item => {
                    return (
                      <Option value={item.id} key="id">
                        {`${item.code} ${item.name}`}
                      </Option>
                    );
                  })}

                </Select>

              )}
            </FormItem>





            <FormItem label="名称">
              {getFieldDecorator('name', {
                //rules: [{ required: true }],
                initialValue: record.name,
              })(
                <Input/>
              )}
            </FormItem>

            <FormItem label="源">
              {getFieldDecorator('origin', {
                //rules: [{ required: true }],
                initialValue: record.origin,
              })(
                <Input />
              )}
            </FormItem>


            <FormItem label="付款说明">
              {getFieldDecorator('reference', {
                //rules: [{ required: true }],
                initialValue: record.reference,
              })(
                <Input />
              )}
            </FormItem>

            <FormItem label="日期">
              {getFieldDecorator('date', {
                rules: [{ required: true }],
                initialValue: record.date ? moment(record.date, 'YYYY-MM-DD') : moment()
              })(<DatePicker />)}
            </FormItem>

            <FormItem label="到期日期">
              {getFieldDecorator('date_due', {
                //rules: [{ required: true }],
                initialValue: record.date_due && moment(record.date_due, 'YYYY-MM-DD')
              })(<DatePicker />)}
            </FormItem>


          </Form>


      </div>

    );
  }
}


export default FormCreate


